﻿/*----------------------------------------------------------------
//  <copyright file="AjaxResultExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-25 17:00</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.AspNetCore
{
    #region "<see cref="AjaxResult"/> 扩展方法"
    /// <summary>
    /// <see cref="AjaxResult"/> 扩展方法
    /// </summary>
    public static class AjaxResultExtensions
    {
        #region "扩展方法"
        #region "将业务操作结果转ajax操作结果，并处理强类型的 <see cref="OperationResult{Data}"/>"
        /// <summary>
        /// 将业务操作结果转ajax操作结果，并处理强类型的 <see cref="OperationResult{Data}"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <param name="dataFunc"></param>
        /// <returns></returns>
        public static AjaxResult ToAjaxResult<T>(this OperationResult<T> result, Func<T, object> dataFunc = null)
        {
            string content = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType type = result.ResultType.ToAjaxResultType();
            object data = dataFunc == null ? result.Data : dataFunc(result.Data);
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion
        #region "将业务操作结果转ajax操作结果，可确定是否包含Data"
        /// <summary>
        /// 将业务操作结果转ajax操作结果，可确定是否包含Data
        /// </summary>
        /// <param name="result">业务操作结果</param>
        /// <param name="containsData">是否包含Data，默认不包含</param>
        /// <returns></returns>
        public static AjaxResult ToAjaxResult(this OperationResult result, bool containsData = false)
        {
            string content = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType type = result.ResultType.ToAjaxResultType();
            return containsData ? new AjaxResult() { Type = type, Content = content, Data = result.Data } : new AjaxResult() { Type = type, Content = content, Data = null };
        }
        #endregion
        #region "将业务操作结果转ajax操作结果，会将 object 类型的 <see cref="OperationResult{Data}"/> 转换为强类型 T，再通过 dataFunc 进行进一步处理"
        /// <summary>
        /// 将业务操作结果转ajax操作结果，会将 object 类型的 <see cref="OperationResult{Data}"/> 转换为强类型 T，再通过 dataFunc 进行进一步处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <param name="dataFunc"></param>
        /// <returns></returns>
        public static AjaxResult ToAjaxResult<T>(this OperationResult result, Func<T, object> dataFunc)
        {
            string content = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType type = result.ResultType.ToAjaxResultType();
            object data = null;
            if (result.Data != null)
            {
                if (dataFunc != null && result.Data is T resultData)
                {
                    data = dataFunc(resultData);
                }
            }
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

        #region "把业务结果类型 <see cref="OperationResultType"/> 转换为Ajax结果类型 <see cref="AjaxResultType"/>"
        /// <summary>
        /// 把业务结果类型 <see cref="OperationResultType"/> 转换为Ajax结果类型 <see cref="AjaxResultType"/>
        /// </summary>
        /// <param name="resultType"></param>
        /// <returns></returns>
        public static AjaxResultType ToAjaxResultType(this OperationResultType resultType)
        {
            return resultType switch
            {
                OperationResultType.Success => AjaxResultType.Success,
                OperationResultType.Error => AjaxResultType.Error,
                OperationResultType.Unauthorized => AjaxResultType.Unauthorized,
                OperationResultType.Forbidden => AjaxResultType.Forbidden,
                OperationResultType.NotFound => AjaxResultType.NotFound,
                OperationResultType.NotAllowed => AjaxResultType.NotAllowed,
                OperationResultType.Unsupported => AjaxResultType.Unsupported,
                OperationResultType.Locked => AjaxResultType.Locked,
                OperationResultType.InternalError => AjaxResultType.InternalError,
                OperationResultType.NotImplemented => AjaxResultType.NotImplemented,
                _ => AjaxResultType.Error,
            };
        }
        #endregion
        #region "判断业务结果类型是否是Failed结果"
        /// <summary>
        /// 判断业务结果类型是否是Failed结果
        /// </summary>
        /// <param name="resultType"></param>
        /// <returns></returns>
        public static bool IsFailed(this OperationResultType resultType)
        {
            return resultType == OperationResultType.Error
                || resultType == OperationResultType.Unauthorized
                || resultType == OperationResultType.Forbidden
                || resultType == OperationResultType.NotFound
                || resultType == OperationResultType.NotAllowed
                || resultType == OperationResultType.Unsupported
                || resultType == OperationResultType.Locked
                || resultType == OperationResultType.InternalError
                || resultType == OperationResultType.NotImplemented;
        }
        #endregion

        #endregion

    }
    #endregion

}
